home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 4 / Meeting Pearls Vol. IV (1996)(GTI - Schatztruhe)[!].iso / Pearls / libs / Pattern / LibMain.c < prev    next >
C/C++ Source or Header  |  1992-01-27  |  2KB  |  103 lines

  1. /* This is only a very simple program which allows you to test the pattern
  2.    routines. Additionally, you see how to use the pattern.library in your
  3.    own programs.
  4. */
  5.  
  6. /*
  7. ** DICE: LibMain:       dcc -2.0 -proto -mRR -ms -mS LibMain.c pattern_lib.lib -o LibMain
  8. **       LibMainNoCase: dcc -2.0 -proto -mRR -ms -mS -D NOCASE LibMain.c pattern_lib.lib -o LibMainNoCase
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <clib/exec_protos.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "pattern.h"
  17.  
  18. /*
  19. #define NOCASE ... to create the program "LibMainNoCase"
  20. */
  21.  
  22.  
  23. /* Base of the pattern.library */
  24.  
  25. void *PatternBase;
  26.  
  27.  
  28. /* Prototypes of the functions defined and used below */
  29.  
  30. void printerror (LONG);
  31. void main (int, char **);
  32.  
  33.  
  34.  
  35. /* Writes the errormessage, that belongs to the number "err" to stdout */
  36.  
  37. void printerror (err)
  38. LONG err;
  39. {
  40.     char buffer[80];
  41.  
  42.     printf ("Error: %s\n", PatternErrorString (err, "default", buffer, sizeof (buffer)));
  43. }
  44.  
  45.  
  46. /* Let's start here! */
  47.  
  48. void main (argc, argv)
  49. int argc;
  50. char **argv;
  51. {
  52.     char s[512];    /* The strings to match should not be longer than 511 Bytes */
  53.     char p[512];    /* The pattern should not be longer than 511 Bytes */
  54.     LONG pm;
  55.     LONG ec;
  56.  
  57.     /* Is there a pattern.library with version 5 or higher? */
  58.  
  59.     if (!(PatternBase=(struct Library *) OpenLibrary (PATLIB_NAME, PATLIB_MIN_VERSION))) {
  60.         printf ("%s V%ld not found!\n", PATLIB_NAME, PATLIB_MIN_VERSION);
  61.         exit (5L);
  62.     }
  63.  
  64.     printf ("The Pattern => ");
  65.     scanf ("%s", p);
  66.     printf ("\nCompiling pattern %s\n", p);
  67.  
  68. #ifdef NOCASE
  69.     if ((pm=AllocPatternNoCase (p, 0L)) > 0L) {
  70. #else
  71.     if ((pm=AllocPattern (p, 0L)) > 0L) {
  72. #endif
  73.         printf ("Please enter now the strings you want to match.\n");
  74.         printf ("Enter '-q' or '+q' to quit!\n");
  75.  
  76.         do {
  77.             printf ("? ");
  78.             scanf ("%s", s);
  79.             ec=MatchThePattern (pm, ((s[0]=='+') || (s[0]=='-')) ? &s[1] : s);
  80.             switch (ec) {
  81.                 case 1L:
  82.                     /* Match found! */
  83.                     printf (":-%c\t\t%s\n", (s[0]=='+') ? ')' : (s[0]=='-') ? 'þ' : '?', s);
  84.                     break;
  85.                 case 0L:
  86.                     /* No match found! */
  87.                     printf (":-%c\t\t\t\t\t%s\n", (s[0]=='-') ? ')' : (s[0]=='+') ? 'þ' : '?', s);
  88.                     break;
  89.                 default:
  90.                     /* Ups. There was an error during matching the pattern */
  91.                     printerror (ec);
  92.             }
  93.         } while (stricmp (s, "q") && stricmp (&s[1], "q"));
  94.         FreePattern (pm);
  95.     }
  96.     else
  97.         printerror (pm);
  98.  
  99.     /* Please don't forget this! */
  100.     CloseLibrary (PatternBase);
  101.     exit (0L);
  102. }
  103.